﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LanguageFeatures.Models;
using System.Text;

namespace LanguageFeatures.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public string Index()
        {
            return "Navigate to a URL to show an example";
        }
        public ViewResult AutoProperty()
        {
            Product myProduct = new Product();
            myProduct.Name = "Kayak";
            string productName = myProduct.Name;
            return View("Result", (object)String.Format("Product name:{0}", productName));
        }
        public ViewResult CreateProduct()
        {
            Product myProduct = new Product();
            myProduct.ProductID = 100;
            myProduct.Name = "Layak";
            myProduct.Description = "a beautiful man";
            myProduct.Category = "Watersports";
            return View("Result", (object)String.Format("Category:{0}",myProduct.Category));
        }
        public ViewResult CreateCollection()
        {
            string[] stringArray = { "apple", "orange", "plum" };
            List<int> intList = new List<int> { 1, 2, 3, 4 };
            Dictionary<string, int> myDict = new Dictionary<string, int> { { "apple", 1 }, { "orange", 2 } };
            return View("Result", (object)stringArray[1]);
        }
        public ViewResult UseExtension()
        {
            ShoppingCart cart = new ShoppingCart
            {
                Products = new List<Product>
                {
                    new Product {Name="Kayak",Price =245M },
                     new Product {Name="Life",Price =24M },
                      new Product {Name="Apaqi",Price =145M }
                }
            };
            decimal cartTotal = cart.TotalPrices();
            return View("Result", (object)string.Format("Total:{0:c}", cartTotal));
        }
        public ViewResult UseFilterExtensionMethod()
        {
            IEnumerable<Product> products = new ShoppingCart
            {
                Products = new List<Product>
                {
                    new Product {Name="Apple",Category="Fruit" ,Price=43M},
                     new Product {Name="Lifejacket",Category="Watersports" ,Price=433M},
                      new Product {Name="Soccer ball",Category="Soccer" ,Price=13.5M},
                       new Product {Name="Corner flag",Category="Soccer" ,Price=1.2M},
                }
            };
            //Func<Product, bool> categoryFilter = delegate (Product prod)
            //   {
            //       return prod.Category == "Soccer";
            //   };
            //Func<Product, bool> categoryFilter = prod => prod.Category == "Soccer";
            decimal total = 0;
            //(prod,count)=>prod.Proce>20 && count>0
            //或者 (prod,count)=>{ //...多条代码语句  return result;}    
            foreach (Product prod in products.Filter(prod=>prod.Category=="Soccer"||prod.Price>20))
            {
                total += prod.Price;
            }
            return View("Result", (object)String.Format("Total:{0}", total));
        }

        public ViewResult createAnonArray()
        {
            var oddsAndEnds = new[]
            {
                new {Name="MVC",Category="Pattern"},
                new {Name="MVVM",Category="You"}
            };
            StringBuilder result = new StringBuilder();
            foreach(var item in oddsAndEnds)
            {
                result.Append(item.Name).Append(item.Category);
            }
            return View("Result", (object)result.ToString());
        }
        public ViewResult FindProducts()
        {
            Product[] products =
            {
                new Product {Name="Apple",Category="Fruit" ,Price=43M},
                     new Product {Name="Lifejacket",Category="Watersports" ,Price=433M},
                      new Product {Name="Soccer ball",Category="Soccer" ,Price=13.5M},
                       new Product {Name="Corner flag",Category="Soccer" ,Price=1.2M},

            };
            //var foundProducts = from match in products orderby match.Price descending select new { match.Name, match.Price };
            var foundProducts = products.OrderByDescending(e => e.Price).Take(3).Select(e => new { e.Name, e.Price });
            int count = 0;
            StringBuilder result = new StringBuilder();
            foreach (var p in foundProducts)
            {
                result.AppendFormat("Price:{0}", p.Price);
                //if (++count == 3)
                //{
                //    break;
                //}
            }
            return View("Result", (object)result.ToString());
        }
    }
}